package com.elm.controller;

import com.elm.po.User;
import com.elm.service.UserService;
import com.elm.util.JwtUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 登录（公开）
    @PostMapping("/getUserByIdByPass")
    public String getUserByIdByPass(@RequestParam String userId, @RequestParam String password) {
        User user = userService.getUserByIdByPass(userId, password);
        if (user == null) {
            throw new RuntimeException("用户名或密码错误");
        }
        // 生成Token返回
        return JwtUtil.generateToken(user.getUserId(), user.getUserType());
    }
    
    @GetMapping("/getUserById")
    public User getUserById(@RequestParam String userId) {
        return userService.getUserById(userId);
    }

    @PostMapping("/createUser")
    public int createUser(@RequestBody User user) {
        return userService.createUser(user);
    }

    @PutMapping("/updateUser")
    public int updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @PutMapping("/updatePassword")
    public int updatePassword(@RequestParam String oldPwd, @RequestParam String newPwd) {
        return userService.updatePassword(oldPwd, newPwd);
    }

    @PutMapping("/updateUserStatus")
    public int updateUserStatus(@PathVariable String targetUserId) {
        return userService.updateUserStatus(targetUserId);
    }

    @PostMapping("/addStaff")
    public int addStaff(@PathVariable String staffUserId) {
        return userService.addStaff(staffUserId);
    }
}